package org.web.clientlistmanager.server.biz;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.web.base.helper.EnumHelper;
import org.web.clientlistmanager.server.domain.ClientProcessDO;
import org.web.clientlistmanager.server.domain.ClientTypeListDO;
import org.web.clientlistmanager.server.domain.query.QueryClientProcess;
import org.web.clientlistmanager.server.domain.query.QueryClientTypeList;
import org.web.clientlistmanager.server.service.ClientProcessService;
import org.web.clientlistmanager.server.service.ClientTypeListService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("clmBatchBiz")
public class ClmBatchBizImpl {

    @Resource
    private ClientTypeListService clientTypeListService;

    @Resource
    private ClientProcessService clientProcessService;


    /**
     * 不考虑使用批量插入。
     */
    @Transactional
    public void saveClientProcessAndClientTypeDetailDTOList(ClientProcessDO clientProcessDO, List<ClientTypeListDO> clientTypeListDOList) {
        clientProcessDO.setProcessLastBeatTime(new Date());
        if (StringUtils.isBlank(clientProcessDO.getClientExtendedValue())) {
            clientProcessDO.setClientExtendedValue("DEFAULT");
        }

        if (clientTypeListDOList != null && clientTypeListDOList.size() > 0) {
            for (ClientTypeListDO clientTypeListDO : clientTypeListDOList) {
                clientTypeListDO.setClientAddress(clientProcessDO.getClientAddress());
                clientTypeListDO.setClientServiceName(clientProcessDO.getClientServiceName());
                clientTypeListDO.setProcessUuid(clientProcessDO.getProcessUuid());
            }
            clientTypeListService.insertClientTypeListBatch(clientTypeListDOList);
        }

        clientProcessService.insertClientProcess(clientProcessDO);

    }


    /**
     * 根据processUuid删除已经注册的信息。 父表ClientProcess 和 子表ClientTypeList信息。
     * 由于数据量不大，故不考虑使用批量删除。
     */
    @Transactional
    public void deleteRegisteredInfoByProcessUuidList(List<String> processUuidList) {

        if (processUuidList != null && processUuidList.size() > 0) {
            QueryClientTypeList queryClientTypeList = new QueryClientTypeList();
            queryClientTypeList.setAllRecords();
            queryClientTypeList.setProcessUuidList(processUuidList);
            List<ClientTypeListDO> list = clientTypeListService.selectClientTypeListList(queryClientTypeList);
            for (ClientTypeListDO clientTypeListDO : list) {
                ClientTypeListDO clientTypeListDOForDelete = new ClientTypeListDO();
                clientTypeListDOForDelete.setClientTypeListId(clientTypeListDO.getClientTypeListId());
                clientTypeListDOForDelete.setIsDelete(EnumHelper.DELETE.Y.name());
                clientTypeListService.updateClientTypeListByClientTypeListId(clientTypeListDOForDelete);
            }

            QueryClientProcess queryClientProcess = new QueryClientProcess();
            queryClientProcess.setAllRecords();
            queryClientProcess.setProcessUuidList(processUuidList);
            List<ClientProcessDO> clientProcessList = clientProcessService.selectClientProcessList(queryClientProcess);
            for (ClientProcessDO clientProcessDO : clientProcessList) {
                ClientProcessDO clientProcessDOForDelete = new ClientProcessDO();
                clientProcessDOForDelete.setClientProcessId(clientProcessDO.getClientProcessId());
                clientProcessDOForDelete.setIsDelete(EnumHelper.DELETE.Y.name());
                clientProcessService.updateClientProcessByClientProcessId(clientProcessDOForDelete);
            }
        }

    }

}
